package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BarChart extends ApplicationFrame {
	private static final long serialVersionUID = -4190956174867527862L;
	private String title = "Sport Planet";
	private String nameXAxis = "";
	private String nameYAxis = "";
	private String seriesName = "";

	public BarChart() {
		super("Sport Planet");
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
	}

	public void showChart(String[] labels, int[] values) {
		final CategoryDataset dataset = createDataset(labels, values,
				seriesName);
		final JFreeChart chart = createChart(dataset, title, nameXAxis,
				nameYAxis);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(chartPanel);

		
		this.pack();
		RefineryUtilities.centerFrameOnScreen(this);
		this.setVisible(true);
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setNameXAxis(String nameXAxis) {
		this.nameXAxis = nameXAxis;
	}

	public void setNameYAxis(String nameYAxis) {
		this.nameYAxis = nameYAxis;
	}

	private CategoryDataset createDataset(String[] labels, int[] values,
			String seriesName) {
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		for (int i = 0; i < labels.length; i++) {
			dataset.addValue(values[i], seriesName, labels[i]);
		}
		return dataset;
	}

	private JFreeChart createChart(final CategoryDataset dataset, String title,
			String nameXAxis, String nameYAxis) {

		// create the chart...
		final JFreeChart chart = ChartFactory.createBarChart(title, nameXAxis,
				nameYAxis, // range axis label
				dataset, // data
				PlotOrientation.HORIZONTAL, // orientation
				false, // include legend
				true, // tooltips?
				false // URLs?
				);

		// set the background color for the chart...
		chart.setBackgroundPaint(Color.white);

		// get a reference to the plot for further customisation...
		final CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);

		// set the range axis to display integers only...
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		// disable bar outlines...
		final BarRenderer renderer = (BarRenderer) plot.getRenderer();
		renderer.setDrawBarOutline(false);

		// set up gradient paints for series...
		final GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue,
				0.0f, 0.0f, Color.lightGray);
		renderer.setSeriesPaint(0, gp0);

		final CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setCategoryLabelPositions(CategoryLabelPositions
				.createUpRotationLabelPositions(Math.PI / 6.0));
		// OPTIONAL CUSTOMISATION COMPLETED.

		return chart;

	}
}
